package com.shuma.product.service.impl;

import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shuma.common.vo.R;
import com.shuma.product.DTO.TopTwentyDTO;
import com.shuma.product.DTO.TypeOrderDTO;
import com.shuma.product.mapper.GoodsDetailInfoMapper;
import com.shuma.product.mapper.GoodsTypeMapper;
import com.shuma.product.mapper.SalesOrExchangeMapper;
import com.shuma.product.pojo.SalesOrExchange;
import com.shuma.product.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author yaoyuan
 * @date 2024年06月18日 下午4:31
 */
@Service
public class DataServiceImpl implements DataService {
    @Autowired
    SalesOrExchangeMapper salesOrExchangeMapper;
    @Autowired
    GoodsDetailInfoMapper detailGoodsMapper;
    @Autowired
    GoodsTypeMapper goodsTypeMapper;
    Log log = Log.get(DataServiceImpl.class);


    @Override
    public R exchangeVolume(LocalDate beginTime, LocalDate endTime) {
        LambdaQueryWrapper<SalesOrExchange> wrapper = new LambdaQueryWrapper<>();
        //查询在给定起始时间与结束时间之间的交易记录
        wrapper.between(SalesOrExchange::getOnTime, beginTime, endTime);
        wrapper.between(SalesOrExchange::getOffTime, beginTime, endTime);
        //如处于多线程环境下 此处不会产生数据不一致
        AtomicReference<Integer> total= new AtomicReference<>(0);
        salesOrExchangeMapper.selectList(wrapper).forEach(salesOrExchange -> total.updateAndGet(v -> v + salesOrExchange.getExchangeCount()));
        //返回全部记录的兑换量之和与当前时间
        return R.ok(new HashMap<String,Object>() {
            {
                put("number", total);
                put("time", LocalDate.now());
            }
        });
    }
    @Override
    public R sale(LocalDate beginTime, LocalDate endTime) {
        LambdaQueryWrapper<SalesOrExchange> wrapper = new LambdaQueryWrapper<>();
        //查询时间内的数据
        wrapper.between(SalesOrExchange::getOnTime, beginTime, endTime);
        wrapper.between(SalesOrExchange::getOffTime, beginTime, endTime);
        List<SalesOrExchange> salesOrExchanges=salesOrExchangeMapper.selectList(wrapper);
        //存储交易记录主键用于后面的查询
        List<Long> ids= new ArrayList<>();
        salesOrExchanges.forEach(salesOrExchange -> ids.add(salesOrExchange.getProductsId()));
        Map<Integer,Integer> integerIntegerMap=new HashMap<>();
        ids.forEach(id->{
            //如果Map内已有相应的分类主键则新建 否则增加value
            if (integerIntegerMap.containsKey(detailGoodsMapper.selectById(id).getGoodsCategoryId())){
                integerIntegerMap.put(detailGoodsMapper.selectById(id).getGoodsCategoryId(),integerIntegerMap.get(detailGoodsMapper.selectById(id).getGoodsCategoryId())+salesOrExchangeMapper.selectById(id).getSales());
            }else {
                // 放分类ID和销量
                integerIntegerMap.put(detailGoodsMapper.selectById(id).getGoodsCategoryId(),salesOrExchangeMapper.selectById(id).getSales());
            }
        });
        List<TopTwentyDTO> topTwentyDTOList=new ArrayList<>();
        List<TopTwentyDTO> finalTopTwentyDTOList = topTwentyDTOList;
        //匹配返回格式
        integerIntegerMap.forEach((k, v)-> finalTopTwentyDTOList.add(new TopTwentyDTO(new Timestamp(new Date().getTime()),v,k.toString())));
        topTwentyDTOList.forEach(topTwentyDTO -> topTwentyDTO.setGoods(goodsTypeMapper.selectById(Integer.valueOf(topTwentyDTO.getGoods())).getCategoryName()));
        //按照销量排序
        topTwentyDTOList.sort((o1, o2) -> o2.getNumber()-o1.getNumber());
        //只截取销量前20的产品类
        if (topTwentyDTOList.size()>20){
            topTwentyDTOList=topTwentyDTOList.subList(0,20);
        }
        return R.ok(topTwentyDTOList);

    }

    @Override
    public R exchangeMethod(LocalDate beginTime, LocalDate endTime) {
        LambdaQueryWrapper<SalesOrExchange> wrapper = new LambdaQueryWrapper<>();
        // 只查询时间内记录
        wrapper.between(SalesOrExchange::getOnTime, beginTime, endTime);
        wrapper.between(SalesOrExchange::getOffTime, beginTime, endTime);
        List<Long> ids= new ArrayList<>();
        salesOrExchangeMapper.selectList(wrapper).forEach(salesOrExchange -> ids.add(salesOrExchange.getProductsId()));
        List<TypeOrderDTO> topTwentyDTOS = new ArrayList<>();
        topTwentyDTOS.add(new TypeOrderDTO(new Timestamp(new Date().getTime()),0,1));
        topTwentyDTOS.add(new TypeOrderDTO(new Timestamp(new Date().getTime()),0,2));
        topTwentyDTOS.add(new TypeOrderDTO(new Timestamp(new Date().getTime()),0,3));
        //每一条数据匹配 交易方式字段 加入结果集
        ids.forEach(id->{
            if (detailGoodsMapper.selectById(id).getPriceType() ==1){
                topTwentyDTOS.get(0).setNumber(topTwentyDTOS.get(0).getNumber()+salesOrExchangeMapper.selectById(id).getSales());
            }
            if (detailGoodsMapper.selectById(id).getPriceType() ==2){
                topTwentyDTOS.get(1).setNumber(topTwentyDTOS.get(1).getNumber()+salesOrExchangeMapper.selectById(id).getSales());
            }
            if (detailGoodsMapper.selectById(id).getPriceType() ==3){
                topTwentyDTOS.get(2).setNumber(topTwentyDTOS.get(2).getNumber()+salesOrExchangeMapper.selectById(id).getSales());
            }
        });
        return R.ok(topTwentyDTOS);
    }

}
